﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallController : MonoBehaviour {

    public float maxSpeed = 5f;

    private Rigidbody rb;
    private Vector3 preV;

	// Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody>();
    }
	
	// Update is called once per frame
	void Update () {
        if (Mathf.Sqrt(rb.velocity.sqrMagnitude) > maxSpeed)
            rb.velocity = rb.velocity.normalized * maxSpeed;
        preV = rb.velocity;
	}

    public void OnCollisionEnter(Collision collision) {
        if (collision.gameObject.CompareTag("wall")) {
            ContactPoint contactPoint = collision.contacts[0];
            Vector3 curDir = preV.normalized;
            Vector3 newDir = Vector3.Reflect(curDir, contactPoint.normal);
            rb.velocity = newDir.normalized * preV.x / preV.normalized.x;
            Debug.Log(curDir);
            Debug.Log(contactPoint.normal);
            Debug.Log(newDir);
        } else if(collision.gameObject.CompareTag("stopWall")) {
            rb.velocity = Vector3.zero;
        }
    }
}
